import java.util.Arrays;

public class Main {
    //移动零
    public static void func1(int[] arr){
        int L = arr.length;
        int cur = 0 , dest = -1;
        while (cur < L){
            if(arr[cur] == 0){
                cur++;
            }else{
                int tmp = arr[cur];
                arr[cur] = arr[dest+1];
                arr[dest+1] = tmp;

                cur++;
                dest++;
            }
        }
    }
    //复写零
    public static void func2(int[] arr){
        int L = arr.length;
        int cur = 0 , dest = -1;
        //找到最后一个值
        while(cur < L){
            if(arr[cur] == 0){
                cur++;
                dest+=2;
            }else{
                cur++;
                dest++;
            }
            if (dest>=L-1){
                break;
            }
        }
        //判断dest的值
        if (dest == L){
            arr[L-1] = 0;
            cur--;
            dest -= 2;
        }
        //开始从后向前复写
        while(cur >= 0){
            if (arr[cur] == 0){
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }else{
                arr[dest--] = arr[cur--];
            }
        }
    }
    public static void func(int[] array){
        int L = array.length;
        int cur = 0,dest = -1;
        //找最后一个复写值
        while(cur < L){
            if(array[cur]!= 0){
                dest++;
            }else{
                dest += 2;
            }
            if(dest >= L-1){
                break;
            }
            cur++;
        }
        //判断dest的位置
        if(dest == L){
            array[L-1] = 0;
            dest-=2;
            cur--;
        }
        while (cur >= 0){
            if(array[cur] == 0){
                array[dest--] = 0;
                array[dest--] = 0;
                cur--;
            }else{
                array[dest--] = array[cur--];
            }
        }
    }
    public static void main(String[] args) {
        int[] arr = {1,2,0,2,0,2,1};
        func(arr);
        System.out.println(Arrays.toString(arr));
     }
}